package com.echo.usedsfu;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.echo.usedsfu.entity.Information;
import com.echo.usedsfu.persistence.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class EditAdServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(EditAdServlet.class.getName());
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws IOException{
		String info_id = req.getParameter("info_id");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(Information.class.getSimpleName(), Long.parseLong(info_id));
		try{
			Information edit_info = pm.getObjectById(Information.class, k);
			if(edit_info==null){
				resp.sendRedirect("/index.jsp");
			}
			HttpSession session = req.getSession();
			session.setAttribute("info_id", edit_info.getInfo_id());
			session.setAttribute("title", edit_info.getTitle());
			session.setAttribute("area", edit_info.getArea());
			session.setAttribute("contact", edit_info.getContact());
			session.setAttribute("description", edit_info.getDescription());
			session.setAttribute("phone", edit_info.getPhone());
			session.setAttribute("email", edit_info.getEmail());
			session.setAttribute("img_id", edit_info.getImg_id());
		}finally{
			pm.close();
		}
		
		resp.sendRedirect("/editAd.jsp");
	}
}
